package org.hgvc.webapp.ets.jsf.beans.agent;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.CommissionPaymentSchema;
import org.hgvc.webapp.ets.bo.CommissionSchema;
import org.hgvc.webapp.ets.bo.Country;
import org.hgvc.webapp.ets.bo.State;
import org.hgvc.webapp.ets.bo.Territory;
import org.hgvc.webapp.ets.constant.AgentConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.AgentService;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.FacesUtils;
import org.jocsb.base.util.SpringUtils;


public class AgentSubAgentBean extends AbstractBean {

	public static final String BEAN_NAME="agentSubAgentBean";
	public static final String NAV_LIST="agentSubAgentList";
	public static final String NAV_CREATE="agentSubAgentCreate";
	public static final String NAV_EDIT="agentSubAgentEdit";
	private Agent agent;
	private Agent parent;
	private AgentService service;
	private List<Agent> subAgentList;
	private List<SelectItem> commSchList;
	private List<SelectItem> commPaymentSchList;
	private String commSchId;
	private String commPaymentSchId;
	private String title;
	private boolean flag = true;
	private String type;
	private String label;
	//

	private MasterSetupService setupService;;
	private String compCountryId;
	private String compStateId;
	private String mailCountryId;
	private String mailStateId;
	private String compTerrId;
	private List<SelectItem> countryList;
	private List<SelectItem> compStateList;
	private List<SelectItem> mailStateList;
	private List<SelectItem> mailSelectList;
	private List<SelectItem> compTerritoryList;
	private String mailBy = CommonConstant.MAIL_SELECT_OTHER;
	private boolean checkFlag = true;
	//
	public void setSetupService(MasterSetupService setupService) {
		this.setupService = setupService;
	}
	public String getMailBy() {
		return mailBy;
	}
	public String getCompCountryId() {
		return compCountryId;
	}
	public void setCompCountryId(String compCountryId) {
		this.compCountryId = compCountryId;
	}
	public String getCompStateId() {
		return compStateId;
	}
	public void setCompStateId(String compStateId) {
		this.compStateId = compStateId;
	}
	public String getMailCountryId() {
		return mailCountryId;
	}
	public void setMailCountryId(String mailCountryId) {
		this.mailCountryId = mailCountryId;
	}
	public String getMailStateId() {
		return mailStateId;
	}
	public void setMailStateId(String mailStateId) {
		this.mailStateId = mailStateId;
	}
	public String getCompTerrId() {
		return compTerrId;
	}
	public void setCompTerrId(String compTerrId) {
		this.compTerrId = compTerrId;
	}
	public boolean isCheckFlag() {
		return checkFlag;
	}
	public void setCheckFlag(boolean checkFlag) {
		this.checkFlag = checkFlag;
	}
	public String getLabel() {
		if(type==null)
			return "";
		return SpringUtils.getMessage(type, new Object[]{});
	}
	public void setLabel(String label) {
		this.label = label;
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public Agent getAgent() {
		return agent;
	}
	public void setAgent(Agent agent) {
		this.agent = agent;
	}
	public Agent getParent() {
		return parent;
	}
	public void setParent(Agent parent) {
		this.parent = parent;
	}
	public String getCommSchId() {
		return commSchId;
	}
	public void setCommSchId(String commSchId) {
		this.commSchId = commSchId;
	}
	public String getCommPaymentSchId() {
		return commPaymentSchId;
	}
	public void setCommPaymentSchId(String commPaymentSchId) {
		this.commPaymentSchId = commPaymentSchId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public void setService(AgentService service) {
		this.service = service;
	}
	/*-------------------------------------------------------------------------------------------------------*/
	public List<SelectItem> getCommSchList() {
		try{
			commSchList = new ArrayList<SelectItem>();
			List<CommissionSchema> list = service.getCommissionSchemaListAgentId(parent.getAgentId());
			for(CommissionSchema cs:list){
				SelectItem item = new SelectItem(cs.getCommSchId(),cs.getCode()+" "+cs.getName());
				commSchList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return commSchList;
	}
	public List<SelectItem> getCommPaymentSchList() {
		try{
			commPaymentSchList = new ArrayList<SelectItem>();
			List<CommissionPaymentSchema> list = service.getCommissionPaymentSchemaListAgentId(parent.getAgentId());
			for(CommissionPaymentSchema cs:list){
				SelectItem item = new SelectItem(cs.getCommPaymentSchId(),cs.getCode()+" "+cs.getName());
				commPaymentSchList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return commPaymentSchList;
	}
	public List<Agent> getSubAgentList() {
		try{
			subAgentList = service.getSubAgent(parent);
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return subAgentList;
	}

	public String navListAction(){
		if(parent==null){
			AgentBean bean = (AgentBean)FacesUtils.getManagedBean(AgentBean.BEAN_NAME);
			parent = bean.getAgent();
			int i = 0;
			for(String t:AgentConstant.AGENT_LEVEL){
				if(t.equalsIgnoreCase(parent.getAgentType())){
					if(i==AgentConstant.AGENT_LEVEL.length-1){
						type = null;
					}else{

						type = AgentConstant.AGENT_LEVEL[i+1];
						break;
					}
				}
				i++;
			}
		}
		return NAV_LIST;
	}

	public String navCreateAction(){
		agent = new Agent();
		return NAV_CREATE;
	}

	public String createAction(){
		try{
			if(type==null)
				throw new BaseException(SpringUtils.getMessage("error.no.auth.create.sub.agent", new Object[]{}));

			agent.setCompCountry(new Country(compCountryId));
			agent.setCompState(new State(compStateId));
			agent.setMailTerritory(new Territory(compTerrId));
			if(!checkFlag){
				agent.setMailAddress1(agent.getCompAddress1());
				agent.setMailAddress2(agent.getCompAddress2());
				agent.setMailCity(agent.getCompCity());
				agent.setMailPostcode(agent.getCompPostcode());
				agent.setMailCountry(agent.getCompCountry());
				agent.setMailState(agent.getCompState());
			}else{
				agent.setMailCountry(new Country(mailCountryId));
				agent.setMailState(new State(mailStateId));
			}
			agent.setStatus(AgentConstant.AGENT_STATUS_SUB_AGENT);
			agent.setRegType(AgentConstant.AGENT_REG_OFFLINE);
			agent.setSchema(new CommissionSchema(this.commSchId));
			agent.setPaymentSchema(new CommissionPaymentSchema(this.commPaymentSchId));
			agent.setAgentType(type);
			service.createSubAgent(agent, parent, getThisUser());
			return NAV_LIST;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}

		return NAV_CREATE;
	}

	public void editActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("agentId");
		String id = (String)param.getValue();
		for(Agent a:subAgentList){
			if(a.getAgentId().equalsIgnoreCase(id)){
				agent = a;
				compCountryId = agent.getCompCountry().getCountryId();
				compStateId = agent.getCompState().getStateId();
				compTerrId =  agent.getMailTerritory().getTerritoryId();
				mailBy = CommonConstant.MAIL_SELECT_OTHER;
				mailCountryId = agent.getMailCountry().getCountryId();
				mailStateId = agent.getMailState().getStateId();
				this.commSchId = agent.getSchema().getCommSchId();
				this.commPaymentSchId = agent.getPaymentSchema().getCommPaymentSchId();
				break;
			}
		}
	}

	public String navEditAction(){
		return NAV_EDIT;
	}

	public String editAction(){
		try{
			if(!checkFlag){
				agent.setMailAddress1(agent.getCompAddress1());
				agent.setMailAddress2(agent.getCompAddress2());
				agent.setMailCity(agent.getCompCity());
				agent.setMailPostcode(agent.getCompPostcode());
				agent.setMailCountry(agent.getCompCountry());
				agent.setMailState(agent.getCompState());
			}else{
				agent.setMailCountry(new Country(mailCountryId));
				agent.setMailState(new State(mailStateId));
			}
			agent.setSchema(new CommissionSchema(this.commSchId));
			agent.setPaymentSchema(new CommissionPaymentSchema(this.commPaymentSchId));
			service.editSubAgent(agent, parent, getThisUser());
			return NAV_LIST;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}

		return NAV_EDIT;
	}

	public String deleteAction(){
		try{
			service.deleteSubAgent(subAgentList,  getThisUser());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_LIST;
	}

	public void viewSubAgentActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("vagentId");
		String id = (String)param.getValue();
		for(Agent a:subAgentList){
			if(a.getAgentId().equalsIgnoreCase(id)){
				parent = a;
				flag = false;
				type = parent.getAgentType();
				break;
			}
		}
	}

	public String viewSubAgentAction(){
		int i = 0;
		for(String t:AgentConstant.AGENT_LEVEL){
			if(t.equalsIgnoreCase(parent.getAgentType())){
				if(i==AgentConstant.AGENT_LEVEL.length-1){
					type = null;
				}else{

					type = AgentConstant.AGENT_LEVEL[i+1];
					break;
				}
			}
			i++;
		}
		return NAV_LIST;
	}

	public String navBackSubAgentAction(){
		parent = parent.getParent();
		AgentBean bean = (AgentBean)FacesUtils.getManagedBean(AgentBean.BEAN_NAME);
		Agent agent = bean.getAgent();
		if(parent.getAgentId().equalsIgnoreCase(agent.getAgentId())){
			flag = true;
		}

		return NAV_LIST;
	}

	public String navBackAction(){
		
		return AgentBean.NAV_PORTAL;
	}

	public List<SelectItem> getCountryList() {
		try{
			countryList = new ArrayList<SelectItem>();
			List<Country> cList = setupService.getCountryList();
			for(Country country:cList){
				SelectItem item = new SelectItem(country.getCountryId(),country.getCode()+" "+country.getDesc());
				countryList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return countryList;
	}
	public List<SelectItem> getMailStateList() {
		try{
			mailStateList = new ArrayList<SelectItem>();
			if(mailCountryId!=null&&mailCountryId.length()>0){
				List<State> cList = setupService.getStateList(mailCountryId);
				for(State state:cList){
					SelectItem item = new SelectItem(state.getStateId(),state.getCode()+" "+state.getDesc());
					mailStateList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return mailStateList;
	}
	public List<SelectItem> getCompStateList() {
		try{
			compStateList = new ArrayList<SelectItem>();
			if(compCountryId!=null&&compCountryId.length()>0){
				List<State> cList = setupService.getStateList(compCountryId);
				for(State state:cList){
					SelectItem item = new SelectItem(state.getStateId(),state.getCode()+" "+state.getDesc());
					compStateList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return compStateList;
	}
	public List<SelectItem> getMailSelectList() {
		mailSelectList = new ArrayList<SelectItem>();
		for(String[] array:CommonConstant.MAIL_COMP_OTHER_SELECT_LIST){
			SelectItem item = new SelectItem(array[0],SpringUtils.getMessage(array[1],new Object[]{}));
			mailSelectList.add(item);
		}

		return mailSelectList;
	}

	public void setMailBy(String mailBy) {
		this.mailBy = mailBy;
		if(mailBy.equalsIgnoreCase(CommonConstant.MAIL_SELECT_COMP)){
			checkFlag = false;
		}else{
			checkFlag = true;
		}
	}
	public List<SelectItem> getCompTerritoryList() {
		try{
			compTerritoryList = new ArrayList<SelectItem>();
			if(compCountryId!=null&&compCountryId.length()>0){
				List<Territory> cList = setupService.getTerritoryList(compStateId);
				for(Territory territory:cList){
					SelectItem item = new SelectItem(territory.getTerritoryId(),territory.getCode()+" "+territory.getDesc());
					compTerritoryList.add(item);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return compTerritoryList;
	}


}
